package com.simtop.hj.utils;

import com.simtop.hj.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;

@Component
public class ScheduledClear {

    @Autowired
    public UserService adminService;

    @Autowired
    private NetPath netPath;


    private Logger logger= LoggerFactory.getLogger(ScheduledClear.class);

    @Scheduled(cron = "0 0 0 * * ?")
    public void clearDataJob(){

        try {
            File file=new File(netPath.getDeletePath());
            for (File listFile : file.listFiles()) {
                if(listFile.exists()){
                    listFile.delete();
                }
            }
            logger.info("删除成功",new Date());
        } catch (Exception e) {
            logger.info("删除失败",new Date());
            e.printStackTrace();
        }


        try {
            adminService.deleteVisitorRole();//凌晨定时删除用户表中的游客账号和游客整合的数据（先删除整合的数据再删除游客账号）
            logger.info("成功定时删除游客用户数据");
        }catch (Exception e){
            logger.info("定时删除游客用户数据");
        }

        try {
            adminService.insertRedisCiYun();
            logger.info("词云数据成功打入缓存");
        }catch (Exception e){
            logger.info("词云数据打入缓存失败");
        }

    }
}
